package com.hkw.servlet;

import com.hkw.model.Student;
import com.hkw.service.IStudentService;
import com.hkw.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 查询学生请求的servlet
 */
@WebServlet(name = "SearchStudentServlet",urlPatterns = "/search")
public class SearchStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String stname = request.getParameter("stname");
        String email = request.getParameter("email");
        String classId = request.getParameter("classId");
        System.out.println(stname + " " + email + " " + classId);
        //实例化接口服务
        IStudentService studentService = new StudentServiceImpl();
        //调用接口方法
        Integer cid = new Integer(-1);
        if(classId != "" && classId != null){
            cid = Integer.parseInt(classId);
        }
        List<Student> list = studentService.findStudentByNameAndEmail(stname, email,cid);
        //如果查找成功，将结果放入request，并且设置当前页，因为共用的一个页面，不设置会出错
        if(list!=null){
            request.setAttribute("stList",list);
            request.setAttribute("reaPage",1);
            //跳转
            request.getRequestDispatcher("main.jsp").forward(request,response);
        }else{
            //查找失败，往request里设值
            request.setAttribute("searcherror","没有符合条件的信息!");
            //跳转
            request.getRequestDispatcher("studentlist").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
